package cmd

import (
	"context"
	"fmt"
	"video-api/internal/controller/content"
	"video-api/internal/controller/gi18n_lang"
	"video-api/internal/controller/goods"
	"video-api/internal/controller/order"
	"video-api/internal/controller/short_drama"
	"video-api/internal/controller/user"
	"video-api/internal/middleware"
	"video-api/utility/queue/job_type"
	"video-api/utility/redis_client"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/hibiken/asynq"
)

var (
	Http = &gcmd.Command{
		Name:        "http",
		Brief:       "start http server",
		Description: "this is the command entry for starting all server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server("http")
			//测试代码
			s.BindHandler("/test", func(r *ghttp.Request) {
				fmt.Println("test===================================")
				redisCfg := redis_client.RedisConfigDefault
				fmt.Println(redisCfg)
				asynqClient := asynq.NewClient(asynq.RedisClientOpt{Addr: redisCfg.Addr, Password: redisCfg.Password, DB: 1})
				tk, err := asynqClient.Enqueue(asynq.NewTask(job_type.ScheduleAddDramaHistory, nil))
				if err != nil {
					fmt.Println(err)
				}
				fmt.Println(tk.ID)

			})

			s.Use(middleware.PrometheusMiddleware)
			// s.Use(ghttp.MiddlewareHandlerResponse)
			// s.Use(middleware.ErrorHandler)
			s.Use(middleware.ResponseHandler)
			s.Group("/api/v1", func(group *ghttp.RouterGroup) {
				group.Middleware(
					middleware.MiddleCORS,
					middleware.JwtMiddleware,
					// middleware.GlobalRateLimiter(),
				)
				group.Group("/shortDrama", func(shortDramaGroup *ghttp.RouterGroup) {
					shortDramaGroup.Bind(short_drama.ShortDramaHandle)
				})
				group.Group("/gi18nLang", func(langGroup *ghttp.RouterGroup) {
					langGroup.Bind(gi18n_lang.LangHandle)
				})
				group.Group("/user", func(userGroup *ghttp.RouterGroup) {
					//userGroup.POST("/create", middleware.RateLimitIPMiddleware, user.UserHandle.CreateUser)
					userGroup.Bind(user.UserHandle)
				})
				// 订单模块
				group.Group("/order", func(orderGroup *ghttp.RouterGroup) {
					orderGroup.Bind(goods.GoodsHandle)
					orderGroup.Bind(order.OrderHandle)
				})
				// 内容模块
				group.Group("/content", func(contentGroup *ghttp.RouterGroup) {
					contentGroup.Bind(content.ContentsHandle)
				})
			})
			s.Run()
			return nil
		},
	}
)
